//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet.Formats.Biff.Tokens
{
    internal sealed class MemFuncToken : FormulaToken
    {
        private readonly AccessorType _type;

        public MemFuncToken(AccessorType type)
        {
            _type = type;
        }

        private ushort _size;

        public override ushort GetLength(FormulaReadContextType formulaReadContextType)
        {
            return 2;
        }

        internal override void ReadContent(FormulaReadContext context)
        {
            _size = context.ReadUInt16();
        }
        public override void WriteContent(FormulaWriteContext context)
        {
            context.WriteUInt16(_size);
        }

        public override FormulaTokenId TokenId
        {
            get { switch(_type)
            {
                case AccessorType.Array:
                    return FormulaTokenId.MemFuncA;
                case AccessorType.Reference:
                    return FormulaTokenId.MemFunc;
                    case AccessorType.Value:
                    return FormulaTokenId.MemFuncV;
                default:
                    throw new NotSupportedException();
            }}
        }

        internal override Expression GetExpression(FormulaEvaluationContext context)
        {
            return context.PoppedArgument().GetExpression(context);
        }
    }
}